package user.aspect;

import core.constants.Constant;
import core.enums.ResultCode;
import core.exception.ServiceException;
import core.threadLocal.ThreadLocalUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import user.domain.user.vo.UserDetailVO;
import user.manager.UserCacheManage;

import java.util.Objects;

@Aspect
@Component
public class UserStatusCheckAspect {
    @Autowired
    private UserCacheManage userCacheManage;

    @Before(value = "@annotation(user.aspect.CheckUserStatus)")
    public void before(JoinPoint joinPoint){
        Long userId = ThreadLocalUtils.get(Constant.USER_ID, Long.class);
        UserDetailVO user = userCacheManage.getUserById(userId);
        if(user == null){
            throw new ServiceException(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        if(Objects.equals(user.getStatus(), Constant.TRUE)){
            throw new ServiceException(ResultCode.FAILED_USER_BANNED);
        }
    }
}
